import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
const _import = require('./_import_production')
import { querystring } from 'vux'
import cookie from 'js-cookie';
import axios from 'axios'

Vue.use(Router)
//获取公司id
function getTenantId(){
	cookie.remove('tanantId');
	var url = window.location.pathname;
	url=url.split('/')
	var tanantId = url[1]
	if(tanantId != ''){
		cookie.set('tanantId', tanantId)
		return tanantId;
	}else{
		cookie.set('tanantId', 1)
		return 1;
	}

}

var tenantId = getTenantId();

const routers = [
	{
		path: '/'+tenantId+'/login',
		name: 'login',
		title: '登录',
		component: _import('login')
	},
  {
    path: '/'+tenantId+'/updatepwd',
    name: 'updatepwd',
    title: '找回密码',
    component: _import('updatepwd')
  },
	{
		path: '/'+tenantId+'/register',
		name: 'register',
		title: '注册',
		component: _import('register')
	},
	{
		path: '/'+tenantId+'/loginout',
		name: 'loginout',
		title: '注销',
		component: _import('loginout')
	},
	{
		path: '/',
		name: 'home',
		title: '首页',
		component: _import('home'),
		redirect: '/'+tenantId,
	},
	{
		path: '/'+tenantId,
		name: 'wechat',
		title: '首页',
		component: _import('home'),
		redirect: '/'+tenantId+'/users/index',
		children : [

			// {
			// 	path: 'shop',
			// 	name: 'shop',
			// 	redirect: 'shop/index',
			// 	component: _import('home'),
			// 	children : [
			// 		{
			// 			path: 'index', title: '商城首页', name: 'shop_index',
			// 			component : _import('shop/shopindex')
			// 		},
			// 		{
			// 			path: 'list', title: '商品列表', name: 'shop_goodslist',
			// 			component : _import('shop/list')
			// 		},
			// 		{
			// 			path: 'goodsinfo/:storeid/:itemid/:goodsid', title: '商品详情', name: 'shop_goodsinfo',
			// 			component : _import('shop/goodsinfo')
			// 		},
			// 		{
			// 			path: 'shopcar', title: '购物车', name: 'shop_card',
			// 			component : _import('shop/shopcar')
			// 		},
			// 		{
			// 			path: 'payment', title: '结算页', name: 'shop_payment',
			// 			component : _import('shop/payment')
			// 		},
			// 		{
			// 			path: 'address', title: '地址列表', name: 'shop_address',
			// 			component : _import('shop/address')
			// 		},
			// 		{
			// 			path: 'updateaddress/:id', title: '修改地址', name: 'shop_updateaddress',
			// 			component : _import('shop/address/form')
			// 		},
			// 		{
			// 			path: 'insertaddress', title: '新增地址', name: 'shop_insertaddress',
			// 			component : _import('shop/address/form')
			// 		},
			// 		{
			// 			path: 'pay/:id', title: '支付', name: 'shop_pay',
			// 			component : _import('shop/payment/pay')
			// 		},
			// 		{
			// 			path: 'goodsclass', title: '商品分类', name: 'shop_goodsclass',
			// 			component : _import('shop/goodsclas')
			// 		},
			// 		{
			// 			path: 'order/:active', title: '订单', name: 'shop_order',
			// 			component : _import('shop/order')
			// 		},
			// 		{
			// 			path: 'orderinfo/:order_id', title: '订单详情', name: 'shop_orderinfo',
			// 			component : _import('shop/order/orderinfo')
			// 		}
			// 	]
			// },

			//会员中心
			{
				path: 'users',
				name: 'users',
				redirect: 'users/index',
				component: _import('home'),
				children : [
					{
						path: 'index', title: '会员中心', name: 'user_index',
						component : _import('user/userindex')
					},
					{
						path: 'userinfo', title: '个人信息', name: 'userinfo',
						component : _import('user/userinfo'),
					},
					{
						path: 'memberquanyi', title: '会员权益', name: 'memberquanyi',
						component : _import('user/memberquanyi'),
					},
					{
						path: 'card', title: '会员卡', name: 'user_card',
						component : _import('user/card'),
					},
					{
						path: 'cardinfo/:id', title: '会员卡详情', name: 'user_cardinfo',
						component : _import('user/cardinfo'),
					},
					{
						path: 'surecard', title: '可领取会员卡', name: 'user_surecard',
						component : _import('user/surecard'),
					},
					{
						path: 'coupon', title: '优惠券', name: 'user_coupon',
						component : _import('user/coupon'),
					},
					{
						path: 'vouchercoupon', title: '领取优惠券', name: 'user_vouchercoupon',
						component : _import('user/vouchercoupon'),
					},

					{
						path: 'sale', title: '消费记录', name: 'sale',
						component : _import('user/sale'),
					},
					{
						path: 'stores', title: '所有分店', name: 'stores',
						component : _import('user/stores'),
					},
					{
						path: 'storeinfo', title: '分店详情', name: 'storeinfo',
						component : _import('user/stores/info'),
					},
					{
						path: 'usersign', title: '会员签到', name: 'usersign',
						component : _import('user/usersign'),
					},
					{
						path: 'vipdepositlist', title: '存款记录', name: 'vipdepositlist',
						component : _import('user/vipdepositlist'),
					},
					{
						path: 'vipfanxianlist', title: '积分返现记录', name: 'vipfanxianlist',
						component : _import('user/vipfanxianlist'),
					},
					{
						path: 'viprechargelist', title: '充值款记录', name: 'viprechargelist',
						component : _import('user/viprechargelist'),
					}
				]
			}
		]
	}
]


const router = new Router({
    mode: 'history',
	routes : [
		...routers
	]
})
const checkopenid = () => {
	if(!cookie.get('openid')){
		let authPage = encodeURIComponent(window.location.href);
		if(!cookie.get('sid')){
			let str = Math.random().toString(16).substr(2,15);
			cookie.set('sid',str,7000);
		}
        var url = 'http://passport.api.lanaoboxiang.com/wechat/bridge?backurl='+authPage+'&sid='+cookie.get('sid');
        window.location.href = url;
	}
}

const checkwhcat = () => {
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i)=="micromessenger") {
		return true;
	} else {
		return false;
	}
}
router.beforeEach((to, from, next) => {
	// // unit.title(to.meta.title);
	if(to.path.indexOf('shop') != -1) store.dispatch('shop/initshopsetting');
	// if(to.path.indexOf('users') != -1) store.dispatch('user/userindex/initdata');
	let path = querystring.parse(window.location.href)
	if(path.mch_openid){
		cookie.set('mch_openid'+tenantId,path.mch_openid,7000);
	}
	if(false){
		if(path.openid){
			cookie.set('openid',path.openid,7000);
			// cookie.set('avatar',path.avatar,7000);//头像
			// cookie.set('access_token',path.access_token,7000);
			// cookie.set('nickname',path.nickname,7000);
		}
		checkopenid();
		if(!localStorage.getItem('user')){
			axios.get('http://passport.api.lanaoboxiang.com/wechat/getTokenByOpenid',{
				params:{
					tenant_id:tenantId,
					openid:cookie.get('openid'),
				}
			}).then(res=>{
				if(!res.data.code){
					if(res.data.data.user.account_type.vip){
						localStorage.setItem('token',res.data.data.api_token);
						localStorage.setItem('refresh_token',res.data.data.refresh_token);
					}else{//跳转到注册（没有vipid）
						next({
							name: 'register'
						});
					}
				}else{
					next({
						name: 'register'
					});
				}
			}).catch(err=>{
				next({
					name: 'register'
				});
			})
		}
	}

	var user = localStorage.getItem('user');
	user= JSON.parse(user)
	if(!user){
	  console.log(to.name)
		if(to.name !== 'login' && to.name !== 'register' && to.name !== 'retrievepass' && to.name !== 'updatepwd'){
		  if(to.name == 'usersign'){//为了判断未登录就去签到，登陆成功后直接到签到页面
        localStorage.setItem('router','usersign');
      }else{
        localStorage.setItem('router','user');
      }
			next({
				name: 'login'
			});
		}else{
			next();
		}
	}else{
		if(to.name === 'login'){
			// unit.title();
			next({
				name: 'user_index'
			});
		}else{
			next()
			// const cookiepermission = localStorage.getItem('permission') || '';
			// if(cookiepermission == 'all') return next();
			// const permission = cookiepermission.split(',').concat(allpermission);
			// const curRouterObj = permission.findIndex(i => i == to.name) == -1;
			// if (curRouterObj) { // 需要判断权限的路由
			// 	next({
			// 		replace: true,
			// 		name: 'error-403'
			// 	});
			// } else { // 没有配置权限的路由, 直接通过
			// 	unit.toDefaultPage([...routers], to.name, router, next);
			// }
		}
	}
});

export default router;
